package org.example.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    // 探讨产生对象获取对象
    private static void a() {
        // 1. 加载xml文件
        ApplicationContext context
                = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2. 获取到对象
        // 得到一个名称为a1的对象 byName:缺点是需要强转
        DemoA a1 = (DemoA) context.getBean("a1");
        // 得到一个类型是DemoA的对象 byType:缺点是只能有一个对象
        // DemoA a2 = context.getBean(DemoA.class);
        // 根据名称和类型获取对象 byName+byType
        DemoA a3 = context.getBean("a2", DemoA.class);
        a1.a();
        // a2.a();
        a3.a();
    }

    // 探讨给对象属性赋值
    private static void b() {
        ApplicationContext bean = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoB b = bean.getBean(DemoB.class);
        DemoC c = bean.getBean(DemoC.class);
        DemoD d = bean.getBean(DemoD.class);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }

    private static void c() {
        ApplicationContext bean = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoE e1 = bean.getBean("e3",DemoE.class);
        System.out.println(e1);
    }

    private static void d() {
        //在加载xml时，已经产生了对象，默认情况下是勤加载方式
        ApplicationContext bean = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoF f = bean.getBean("f", DemoF.class);

    }

   private static void e() {
        //默认情况下单例模式
        ApplicationContext bean = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoG g1 = bean.getBean("g",DemoG.class);
        DemoG g2 = bean.getBean("g",DemoG.class);
        System.out.println(g1);
        System.out.println(g2);
        System.out.println(g1==g2);
    }

    public static void main(String[] args) {
        e();
    }
}
